package org.lbm.flow.node;

import org.lbm.flow.agent.AgentResult;

import java.util.Map;
import java.util.HashMap;

/**
 * 节点执行结果
 *
 * @author insist
 * @since 2025-08-01
 */
public class NodeResult {

    public enum Status {
        SUCCESS,    // 成功
        FAILED,     // 失败
        SKIPPED,    // 跳过
        TIMEOUT,    // 超时
        CANCELLED   // 取消
    }

    private final String nodeId;
    private final Status status;
    private final Object data;
    private final String message;
    private final Map<String, Object> metadata;
    private final long executionTime;
    private final AgentResult agentResult;

    private NodeResult(String nodeId, Status status, Object data, String message,
                      Map<String, Object> metadata, long executionTime, AgentResult agentResult) {
        this.nodeId = nodeId;
        this.status = status;
        this.data = data;
        this.message = message;
        this.metadata = metadata != null ? new HashMap<>(metadata) : new HashMap<>();
        this.executionTime = executionTime;
        this.agentResult = agentResult;
    }

    /**
     * 创建成功结果
     */
    public static NodeResult success(String nodeId, Object data) {
        return new NodeResult(nodeId, Status.SUCCESS, data, "节点执行成功", null, 0, null);
    }

    /**
     * 创建成功结果
     */
    public static NodeResult success(String nodeId, Object data, String message) {
        return new NodeResult(nodeId, Status.SUCCESS, data, message, null, 0, null);
    }

    /**
     * 创建失败结果
     */
    public static NodeResult failed(String nodeId, String message) {
        return new NodeResult(nodeId, Status.FAILED, null, message, null, 0, null);
    }

    /**
     * 创建失败结果
     */
    public static NodeResult failed(String nodeId, String message, Throwable cause) {
        return new NodeResult(nodeId, Status.FAILED, cause, message, null, 0, null);
    }

    /**
     * 创建跳过结果
     */
    public static NodeResult skipped(String nodeId, String reason) {
        return new NodeResult(nodeId, Status.SKIPPED, null, reason, null, 0, null);
    }

    /**
     * 创建超时结果
     */
    public static NodeResult timeout(String nodeId, String message) {
        return new NodeResult(nodeId, Status.TIMEOUT, null, message, null, 0, null);
    }

    /**
     * 创建取消结果
     */
    public static NodeResult cancelled(String nodeId, String reason) {
        return new NodeResult(nodeId, Status.CANCELLED, null, reason, null, 0, null);
    }

    /**
     * 从智能体结果创建节点结果
     */
    public static NodeResult fromAgentResult(String nodeId, AgentResult agentResult) {
        Status status;
        switch (agentResult.getStatus()) {
            case SUCCESS:
                status = Status.SUCCESS;
                break;
            case FAILED:
                status = Status.FAILED;
                break;
            case SKIPPED:
                status = Status.SKIPPED;
                break;
            case TIMEOUT:
                status = Status.TIMEOUT;
                break;
            default:
                status = Status.FAILED;
        }

        return new NodeResult(nodeId, status, agentResult.getData(),
                            agentResult.getMessage(), agentResult.getMetadata(),
                            agentResult.getExecutionTime(), agentResult);
    }

    /**
     * 设置执行时间
     */
    public NodeResult withExecutionTime(long executionTime) {
        return new NodeResult(nodeId, status, data, message, metadata, executionTime, agentResult);
    }

    /**
     * 添加元数据
     */
    public NodeResult withMetadata(String key, Object value) {
        Map<String, Object> newMetadata = new HashMap<>(metadata);
        newMetadata.put(key, value);
        return new NodeResult(nodeId, status, data, message, newMetadata, executionTime, agentResult);
    }

    /**
     * 获取节点ID
     */
    public String getNodeId() {
        return nodeId;
    }

    /**
     * 获取状态
     */
    public Status getStatus() {
        return status;
    }

    /**
     * 获取数据
     */
    @SuppressWarnings("unchecked")
    public <T> T getData() {
        return (T) data;
    }

    /**
     * 获取消息
     */
    public String getMessage() {
        return message;
    }

    /**
     * 获取元数据
     */
    public Map<String, Object> getMetadata() {
        return new HashMap<>(metadata);
    }

    /**
     * 获取执行时间
     */
    public long getExecutionTime() {
        return executionTime;
    }

    /**
     * 获取智能体结果
     */
    public AgentResult getAgentResult() {
        return agentResult;
    }

    /**
     * 检查是否成功
     */
    public boolean isSuccess() {
        return status == Status.SUCCESS;
    }

    /**
     * 检查是否失败
     */
    public boolean isFailed() {
        return status == Status.FAILED;
    }

    /**
     * 检查是否跳过
     */
    public boolean isSkipped() {
        return status == Status.SKIPPED;
    }

    /**
     * 检查是否超时
     */
    public boolean isTimeout() {
        return status == Status.TIMEOUT;
    }

    /**
     * 检查是否取消
     */
    public boolean isCancelled() {
        return status == Status.CANCELLED;
    }

    @Override
    public String toString() {
        return String.format("NodeResult{nodeId='%s', status=%s, message='%s', executionTime=%dms}",
                           nodeId, status, message, executionTime);
    }
}
